package com.polimi.meteocal.security;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import com.polimi.meteocal.business.control.CalendarManager;
import com.polimi.meteocal.business.control.EventManager;
import com.polimi.meteocal.business.control.InviteManager;
import com.polimi.meteocal.business.control.UserManager;
import com.polimi.meteocal.business.entity.Invite;
import com.polimi.meteocal.business.entity.Notification;

@ManagedBean
@RequestScoped
public class InizializationBean {
/*
 * This Bean is linked to calendarPage (notification dialog). InizializationBean checks 
 * if there are invites that the user hasn't already seen and notifies it
 */
	@EJB
	InviteManager im;
	@EJB
	UserManager um;
	@EJB
	CalendarManager cm;
	@EJB
	EventManager em;
	
	//String that contains the conditional message to be displayed in the dialog
	private String msg;
	
	//List of invites that the user hasn't already seen
	private List<Invite> unreadInvites;
	
	//Support variable for the visibility of the dialog in xhtml page
	private boolean visibility;
	
	//Public visibility of the calendar
	private boolean calendarVisibility;
	
	
	@PostConstruct
	public void init(){
		this.calendarVisibility= cm.getCalendarPrivacy(um.getLoggedUser().getEmail());
		System.out.println("INIZIALIZATION BEAN-calendar visibility before set: "+this.calendarVisibility);
		
		//Checks if the user is coming from Login page to decide the visibility of the notification dialog to be shown
		FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
        ServletContext sContext = request.getSession().getServletContext();
        this.visibility= (boolean) sContext.getAttribute("justLogged");
        sContext.setAttribute("justLogged", false);
        
        //Build the message to display in notification dialog
		this.unreadInvites= im.getUnreadInvites(um.getLoggedUser().getEmail());
		int count= this.unreadInvites.size();
		if(count>1)
			this.msg= "You have "+count+" new invites: check your invite list";
		else if(count==1)
			this.msg= "You have "+count+" new invite: check your invite list";
		else
			this.msg= "You don't have UNREAD invites";
		
	}
	
	public void markAsRead(){
		//Pressing the OK button in the dialog, the user set the attrubute isRead of
		//the entity invite (the invite will not be unread anymore)
		
		
		for(Invite i: unreadInvites){
			
			i.setRead(true);
		}
		im.setInviteReadMark(unreadInvites);
		
		//Get weather notifications from weatherBean
		FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
        ServletContext sContext = request.getSession().getServletContext();
		List<Notification> unreadNotifications= (List<Notification>) sContext.getAttribute("unreadNotifications");
		for(Notification n: unreadNotifications){
			n.setIsREAD(true);
		}
		em.setNotificationReadMark(unreadNotifications);
	}
	
	public void storeCalendarVisibility(){
		System.out.println("INIZIALIZATION BEAN-calendar visibility after set: "+this.calendarVisibility);
		cm.setCalendarPrivacy(this.calendarVisibility);
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public List<Invite> getUnreadInvites() {
		return unreadInvites;
	}

	public void setUnreadInvites(List<Invite> unreadInvites) {
		this.unreadInvites = unreadInvites;
	}

	public boolean isVisibility() {
		return visibility;
	}

	public void setVisibility(boolean visibility) {
		this.visibility = visibility;
	}

	public boolean isCalendarVisibility() {
		return calendarVisibility;
	}

	public void setCalendarVisibility(boolean calendarVisibility) {
		this.calendarVisibility = calendarVisibility;
	}

}
